package cn.wolfcode.wolf2w.job;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.util.RedisKeys;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import java.util.Set;
//@Component
public class RedisDataPersistenceJob {
    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;
    @Autowired
    private StringRedisTemplate template;
    @Autowired
    private IStrategyService strategyService;
    @Scheduled(cron="0/5 * * * * ? ")
    public void dowork(){
        //持久化攻略统计对象
        String partten= RedisKeys.STRATEGY_STATIC_VO.join("*");
        Set<String> keys = template.keys(partten);
        for (String key : keys) {
            String vostr = template.opsForValue().get(key);
            StrategyStatisVO vo = JSON.parseObject(vostr, StrategyStatisVO.class);
            Strategy strategy = strategyService.get(vo.getStrategyId());
            BeanUtils.copyProperties(vo,strategy);
            strategyService.update(strategy);
        }
    }
}
